/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.preview
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.utils.Timer;
	
	import multimedia.control.event.preview.LoadSlideEvent;
	import multimedia.model.ModelLocator;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	
	import org.osmf.elements.SlideElement;
	import org.osmf.events.LoadEvent;
	import org.osmf.traits.LoadState;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;
	import org.osmf.traits.PlayTrait;
	
	public class LoadSlideCommand extends SequenceCommand implements ICommand
	{

		private var e:LoadSlideEvent;
		private var model:ModelLocator;
		private var slide:SlideElement;

		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.e = event as LoadSlideEvent;
			this.nextEvent = e.nextEvent;

			model.numberOfSlides = e.slide.slideSteps.length;
			model.currentSlide = 1;

			var height:int = FlexGlobals.topLevelApplication.stage.height * 0.9;
			var width:int = (e.slide.aspect_ratio == "16:9" ? height * (16 / 9) : height * (4 / 3));
			
			slide = new SlideElement(e.slide);
			(slide.getTrait(MediaTraitType.LOAD) as LoadTrait).load();
			(slide.getTrait(MediaTraitType.LOAD) as LoadTrait).addEventListener(LoadEvent.LOAD_STATE_CHANGE, loadStateChange);
			
			//for resolution not 16/9
			//we need to start with the width
			if (width > FlexGlobals.topLevelApplication.stage.width)
			{
				width = FlexGlobals.topLevelApplication.stage.width * 0.9;
				height = (e.slide.aspect_ratio == "16:9" ? width / (16 / 9) : width / (4 / 3));
			}
			
			model.wrapper.height = height;
			model.wrapper.width = width;
			model.wrapper.x = FlexGlobals.topLevelApplication.stage.width * 0.5 - width * 0.5;
			model.wrapper.y = FlexGlobals.topLevelApplication.stage.height * 0.5 - height * 0.5;

			model.wrapper.mediaPlayer.autoPlay = false;
			
			model.wrapper.media = slide;
			model.wrapper.addMediaElement(slide);
			
			executeNextCommand();
		}
		
		private function loadStateChange(event:LoadEvent):void
		{
			if(event.loadState == LoadState.READY)
			{
				(slide.getTrait(MediaTraitType.PLAY) as PlayTrait).play();
				(slide.getTrait(MediaTraitType.PLAY) as PlayTrait).pause();
			}
		}

	}
}
/* package multimedia.control.command.preview
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import multimedia.control.event.preview.LoadSlideEvent;
	import multimedia.model.ModelLocator;
	
	import mx.core.Application;
	
	import org.osmf.display.ScaleMode;
	import org.osmf.events.LoadEvent;
	import org.osmf.layout.LayoutUtils;
	import org.osmf.media.URLResource;
	import org.osmf.slide.SlideElement;
	import org.osmf.swf.SWFElement;
	import org.osmf.swf.SWFLoader;
	import org.osmf.traits.LoadState;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;
	import org.osmf.utils.URL;

	public class LoadSlideCommand extends SequenceCommand implements ICommand
	{

		private var e:LoadSlideEvent;
		private var model:ModelLocator;

		private var slide:SlideElement; 
		private var swf:SWFElement;
		
		private var height:int;
		private var width:int;

		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.e = event as LoadSlideEvent;
			this.nextEvent = e.nextEvent;
			
			height = FlexGlobals.topLevelApplication.stage.height * 0.9;
			width = (e.slide.aspect_ratio == "16:9" ? height * (16 / 9) : height * (4 / 3));

			//for resolution not 16/9
			//we need to start with the width
			if (width > FlexGlobals.topLevelApplication.stage.width)
			{
				width = FlexGlobals.topLevelApplication.stage.width * 0.9;
				height = (e.slide.aspect_ratio == "16:9" ? width / (16 / 9) : width / (4 / 3));
			}
			
			slide = new SlideElement(e.slide.url, e.slide.slideSteps, e.slide.aspect_ratio);
			var load:LoadTrait = slide.getTrait(MediaTraitType.LOAD) as LoadTrait;
			load.addEventListener(LoadEvent.LOAD_STATE_CHANGE, onLoadState);
			
			swf = new SWFElement(new SWFLoader(), new URLResource(new URL("Slide1.swf")));
			
			model.wrapper.x = FlexGlobals.topLevelApplication.stage.width * 0.5 - width * 0.5;
			model.wrapper.y = FlexGlobals.topLevelApplication.stage.height * 0.5 - height * 0.5; 
			
			LayoutUtils.setAbsoluteLayout(slide.metadata, width, height,0,0);
			LayoutUtils.setLayoutAttributes(slide.metadata,ScaleMode.NONE);
			
			model.wrapper.width = width;
			model.wrapper.height = height; 

			model.wrapper.element = slide;
			
			model.wrapper.mediaPlayer.autoPlay = false;
			
			executeNextCommand();
		}
		
		private function onLoadState(event:LoadEvent):void
		{
			if(event.loadState == LoadState.READY)
			{ 
				//var view:ViewTrait = slide.getTrait(MediaTraitType.VIEW) as ViewTrait;

			}
			
		}
		
	}
} */